package cn.ecut.dzy.thread;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @program: java-interview
 * @description:
 * @author: DingZhenYun
 * @create: 2021-10-22 14:59
 **/

public class ArrayListNotSafeDemo {
    public static void main(String[] args) {
        Collections.synchronizedMap(new HashMap<>());
        Map<String,Object> map =new ConcurrentHashMap<>();
        for (int i = 1; i <=30 ; i++) {
            new Thread(() -> {
                map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0, 8));
                System.out.println(map);
            }, String.valueOf(i)).start();
        }
    }

    private static void setnosafe() {
//        Set<String> set = new HashSet<>();不安全
        //Set<String> set = Collections.synchronizedSet(new HashSet<>());
        Set<String> set = new CopyOnWriteArraySet<>();


        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }

    private static void listnosafe() {
        //单线程环境的ArrayList是不会有问题的
//        List<String> list = new ArrayList<>();
//        list.add("a");
//        list.add("b");
//        list.add("c");
//        for(String element : list) {
//            System.out.println(element);
//        }

        //集合类线程不安全举例
//        List<String> list1 = new ArrayList<>();
        List<String> list1 = new Vector<>();//方法1
        //方案二：Collections.synchronizedList()
        List<String> list2 = Collections.synchronizedList(new ArrayList<>());
        //方案三
        /**读写分离的思想
         * 写时复制 copyOnWrite 容器即写时复制的容器 往容器添加元素的时候,不直接往当前容器object[]添加,而是先将当前容器object[]进行
         * copy 复制出一个新的object[] newElements 然后向新容器object[] newElements 里面添加元素 添加元素后,
         * 再将原容器的引用指向新的容器 setArray(newElements);
         * 这样的好处是可以对copyOnWrite容器进行并发的读,而不需要加锁 因为当前容器不会添加任何容器.所以copyOnwrite容器也是一种
         * 读写分离的思想,读和写不同的容器.
         *          public boolean add(E e) {
         *         final ReentrantLock lock = this.lock;
         *         lock.lock();
         *         try {
         *             Object[] elements = getArray();
         *             int len = elements.length;
         *             Object[] newElements = Arrays.copyOf(elements, len + 1);
         *             newElements[len] = e;
         *             setArray(newElements);
         *             return true;
         *         } finally {
         *             lock.unlock();
         *         }
         *     }
         * @param args
         */
        List<String> list3 = new CopyOnWriteArrayList();

        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                list3.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(list3);
            }, String.valueOf(i)).start();
        }
    }
}
